package com.financing.platform.web.api;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.OrderManagerUserDTO;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.service.IOrderManagerUserService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.vo.OrderEmailVO;
import com.financing.platform.vo.OrderManagerUserVO;

@Controller("orderManagerUserApi")
@RequestMapping("/api/order")
@Scope("prototype")
public class OrderManagerUserAction extends BaseAction {

	@Autowired
	private IOrderManagerUserService orderManagerUserServiceImpl;

	@RequestMapping("/queryNotOrderManagerUserList.shtml")
	@ResponseBody
	public ResultMap queryNotOrderManagerUser(HttpServletRequest request, HttpServletResponse response, OrderManagerUserVO user, Pager page) {
		try {
			int count = orderManagerUserServiceImpl.queryNotOrderManagerUsersCount(user);

			// page.setTotalCount(count);
			// user.setBegin(page.getStartIndex());
			// user.setEnd(page.getPageSize());

			List<OrderManagerUserVO> list = null;

			if (count > 0) {
				list = orderManagerUserServiceImpl.queryNotOrderManagerUsers(user);
			}

			resultMap.setAttribute("list", list);
			resultMap.setAttribute("page", page);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (SQLException e) {
			log.error("queryNotOrderManagerUser error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}

	@RequestMapping("/queryOrderManagerUsersList.shtml")
	@ResponseBody
	public ResultMap queryOrderManagerUsers(HttpServletRequest request, HttpServletResponse response, OrderManagerUserVO user, Pager page) {
		try {
			int count = orderManagerUserServiceImpl.queryOrderManagerUsersCount(user);

			page.setTotalCount(count);

			user.setBegin(page.getStartIndex());
			user.setEnd(page.getPageSize());

			List<OrderManagerUserVO> list = null;

			if (count > 0) {
				list = orderManagerUserServiceImpl.queryOrderManagerUsers(user);
			}

			resultMap.setAttribute("list", list);
			resultMap.setAttribute("page", page);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (SQLException e) {
			log.error("queryOrderManagerUsers error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}
	
	@RequestMapping("/orderManagerUserAdd.shtml")
	@ResponseBody
	public ResultMap orderManagerUserAdd(HttpServletRequest request, HttpServletResponse response, OrderManagerUserDTO user){
		try {
			if(user != null && user.getSystemUserId() != null && !"".equals(user.getSystemUserId())){
				DTOdateSet.setCreateInfo(user, request.getSession());
				DTOdateSet.setUpdateInfo(user, request.getSession());
				OrderManagerUserVO tmpUser = new OrderManagerUserVO();
				tmpUser.setSystemUserId(user.getSystemUserId());
				
				List<OrderManagerUserVO> list = orderManagerUserServiceImpl.queryOrderManagerUsers(tmpUser);
				if (list != null && list.size() > 0) {
					resultMap.setStatus(STATUS.FAILE);
					resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				}else {
					orderManagerUserServiceImpl.addOrderManagerUser(user);
					
					resultMap.setStatus(STATUS.SUCCESS);
					resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
				}
				
			}else {
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
			}
		} catch (Exception e) {
			log.error("orderManagerUserAdd error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	
	@RequestMapping("/orderReminder.shtml")
	@ResponseBody
	public ResultMap orderReminder(HttpServletRequest request, HttpServletResponse response){
		try{
			HttpSession session = request.getSession();
			UserInfoDTO user = (UserInfoDTO)session.getAttribute(DTOdateSet.user);
			String target = request.getParameter("target");
			UserInfoDTO usertwo=new UserInfoDTO();
			if("1".equals(target)){
				usertwo.setUserId(null);
			}else{
				usertwo.setUserId(user.getUserId());
			}
			List<OrderEmailVO> list = orderManagerUserServiceImpl.queryUserNeedTODOOrder(usertwo);
			resultMap.setAttribute("list",list );
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("orderReminder error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	 
}
